package cn.mesmile.code.controller;

import cn.mesmile.code.cache.CacheUtil;
import cn.mesmile.code.common.R;
import cn.mesmile.code.event.ErrorLogEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zb
 * @date 2021/5/10 11:25
 * @Description
 */
@RequestMapping("/api/v1/message")
@RestController
public class MessageController {

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/test")
    public R test(){
        String s = CacheUtil.get("test:", "test2:", "test3:", () -> "我测试一下");
        return R.data(s);
    }

    /**
     *
     *  已验证： 集群情况下 --》 springboot 自带的监听器，不会重复消费；
     *           哪一个端口发送的消息，就只会被那个端口消费
     *
     * @return
     */
    @GetMapping("/send")
    public Map<String,Object> send(){
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("test", "testOne");
        resultMap.put("test2", "testTwo");
        resultMap.put("result","success");
        ErrorLogEvent errorLogEvent = new ErrorLogEvent(resultMap);
        // 发送消息
        applicationContext.publishEvent(errorLogEvent);
        return resultMap;
    }
}
